using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using Yoozoo.Framework.Core;

namespace WorldMapBase.Tile.Pool
{
    public class PoolCollectionManager: SingletonMono<PoolCollectionManager>
    {

        private int _lastPoolCount = 0;
        [SerializeField]
        private int PoolCount = 0;

        public List<ObjectPoolBase> AllObjectPool = new List<ObjectPoolBase>(320);

        
        /// <summary>
        /// 上帧收缩的索引
        /// </summary>
        private int _lastShrinkIndex = -1;
        /// <summary>
        /// 单帧收缩的池数量
        /// </summary>
        private int _shrinkCount = 2;
        
        public void AddObjectPool(ObjectPoolBase poolBase)
        {
            if (poolBase == null)
            {
                Debug.LogWarning("poolBase is null");
                return;
            }
            if (AllObjectPool == null)
            {
                Debug.LogWarning("AllObjectPool is null");
                return;
            }
           
            AllObjectPool.Add(poolBase);
            PoolCount++;
        }


        public void RemoveObjectPool(ObjectPoolBase poolBase)
        {
            if (poolBase == null)
            {
                Debug.LogWarning("poolBase is null");
                return;
            }
            if (AllObjectPool == null)
            {
                Debug.LogWarning("AllObjectPool is null");
                return;
            }
            if (AllObjectPool.Remove(poolBase))
            {
                PoolCount--;
            }
        }

        public  void Initialize(Options options = null)
        {
            Debug.Log("PoolCollectionManager");
        }
        private void Update()
        {

#if UNITY_EDITOR
            if (_lastPoolCount!=PoolCount)
            {
                AllObjectPool.Sort((a, b) => { return a._name.CompareTo(b._name); });   
                _lastPoolCount = PoolCount;
            }
#endif


            if (PoolCount == 0)
            {
                return;
            }
            
            float currentTime = Time.time;

            if (_lastShrinkIndex >= PoolCount - 1)
            {
                _lastShrinkIndex = -1;
            }

            int shrinkEndIndex = _lastShrinkIndex + _shrinkCount;
            if (shrinkEndIndex >= PoolCount)
            {
                shrinkEndIndex = PoolCount - 1;
            }

            for (int i = _lastShrinkIndex + 1; i <= shrinkEndIndex; i++)
            {
                AllObjectPool[i].Shrink(currentTime);
            }

#if UNITY_EDITOR
            for (int i = 0; i < AllObjectPool.Count; i++)
            {
                var pool = AllObjectPool[i];
                var newUsePercent = (float)pool.AliveCount / (pool.AliveCount + pool.PoolCount);
                if (pool.UsePercent == 0)
                {
                    pool.UsePercent = newUsePercent;
                }
                else
                {
                    if (newUsePercent!=0)
                    {
                        pool.UsePercent = (pool.UsePercent + newUsePercent) / 2;
                    }
                }
            }
#endif
            _lastShrinkIndex = shrinkEndIndex;
        }

        private void OnDestroy()
        {
            var _list = AllObjectPool;
            AllObjectPool = null;
            foreach (var pool in _list)
            {
                if (pool != null)
                {
                    pool.Destroy();
                } 
            }
        }
        
        public void Log()
        {
            #if UNITY_EDITOR
            for (int i = 0; i < AllObjectPool.Count; i++)
            {
                var pool = AllObjectPool[i];
                if (pool.idleMaxCount < pool.maxPoolCount)
                {
                    UnityEngine.Debug.LogError(pool._name + " 空闲最大数量:" + pool.idleMaxCount + " 实际:" + pool.maxPoolCount + " 额外创建" + pool.extraCreatedCount + " 利用率" + pool.UsePercent);
                }
                else
                {
                    UnityEngine.Debug.LogError(pool._name + " 空闲最大数量:" + pool.idleMaxCount + " 实际:" + pool.maxPoolCount + " 额外创建" + pool.extraCreatedCount + " 利用率" + pool.UsePercent);
                }
            }
            #endif
        }
    }
}